package ua.tach.shortl;

import ua.tach.shortl.logic.Country;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Vector3;

public class Input implements InputProcessor {
	public final Vector3 vector;
	public int key;
	public boolean handled;
	public int newRail;

	public Input () {
		vector = new Vector3();
	}

	private void toReal (int x, int y) {
		vector.set( (x-Renderer.RAILMENU_SIZEX)*10.0f / (Renderer.screenSizeX-Renderer.RAILMENU_SIZEX) , y*10.0f / (Renderer.screenSizeY-Renderer.BANNER_SIZEY), 0);
	}

	@Override
	public boolean keyDown(int keycode) {
		key = keycode;
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		System.out.println("Real X= "+ x +" Y=" + y);
		if (Country.gamePause) {
			Country.gamePause = false;
			Country.playMusic(false);
			System.out.println("Pause OFF ");
			return false;
		}
		toReal(x, y);
		if(x < Renderer.RAILMENU_SIZEX && y < Renderer.screenSizeY-Renderer.BANNER_SIZEY) {
			// Request new rail
			Country.flyingRail.downFlRail( y*6/(Renderer.screenSizeY-Renderer.BANNER_SIZEY) +1 , vector);
		} else {
			if(y > Renderer.screenSizeY-Renderer.BANNER_SIZEY) {
				//Country.addCity();
			} else {
				// Click on country
				if(Country.flyingRail.downCountry( vector)) {
					// Click on switch
					Country.clickHandler(x, y);
				}
			}
		}
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		//System.out.println("Real X= "+ x +" Y=" + y);
		toReal(x, y);
		Country.flyingRail.dragFlRail(vector);
		//handled = false;
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		Country.flyingRail.upFlRail(vector);
		return false;
	}

	public void tick () {
		handled = true;
		key = 0;
	}

}
